
package com.jackchong.utils;

import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.viewpager.widget.PagerAdapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.jackchong.base.BaseToolsActivity;
import com.utils.R;

import java.util.List;

/**
 * 作者: jack(黄冲)
 * 邮箱: 907755845@qq.com
 * create on 2018/12/29 13:29
 */

public abstract class JPAdapter<T> extends PagerAdapter {

	private List<T> mList;
	private int mLayout;
	protected BaseToolsActivity mActivity;

	public JPAdapter(List<T> list, @LayoutRes int layout) {
		mList = list;
		mLayout = layout;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}


	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		mActivity = (BaseToolsActivity) container.getContext();
		View view = LayoutInflater.from(container.getContext()).inflate(mLayout, container, false);
		container.addView(view);
		JAuto.auto(view);
		JPAdapter.ViewHelper viewHolder = new JPAdapter.ViewHelper(view);
		convert(viewHolder, mList.get(position), position);
		return view;
	}

	public void setData(int index, T bean){
		mList.set(index, bean);
	}

	public void setNewData(List<T> list){
		mList = list;
	}

	public List<T> getData() {
		return mList;
	}

	@Override
	public int getCount() {
		return mList == null ? 0 : mList.size();
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	protected abstract void convert(ViewHelper helper, T bean, int position);


	public static class ViewHelper{

		public View contentView;
		public BaseToolsActivity context;

		public ViewHelper(View itemView) {
			contentView = itemView;
			context = (BaseToolsActivity) itemView.getContext();
		}

		public <V extends View> V getView(@IdRes int id){
			return ((V) contentView.findViewById(id));
		}

		public void setText(@IdRes int id, String text){
			((TextView) getView(id)).setText(text);
		}

		public String getString(@IdRes int id){
			return getTV(id).getText().toString();
		}

		public void setText(@IdRes int id, int text){
			setText(id, String.valueOf(text));
		}

		public void setImageResource(@IdRes int id, @DrawableRes int res){
			((ImageView) getView(id)).setImageResource(res);
		}

		public TextView getTV(@IdRes int id){
			return getView(id);
		}

		public ImageView getIV(@IdRes int id){
			return getView(id);
		}

		public <V extends View>V getView(@IdRes int id, Class<V> cls){
			return ((V) getView(id));
		}


		public void setImageUrl(@IdRes int id, String url){
			setImageUrl(id, url, 0);
		}

		public void setImageUrl(@IdRes int id, String url, @DrawableRes int perch) {
			RequestOptions options = new RequestOptions()
					.error(R.drawable.icon_fail)
					.placeholder(perch);
			Glide.with(contentView)
					.load(url)
					.apply(options)
					.into(getIV(id));
		}
	}
}